package com.bonc.modules.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.common.dao.CommonDao;
import com.bonc.modules.common.entity.Region;
import com.bonc.modules.common.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jinlong
 * @date 2021/6/29
 */
@DataSource("data")
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private CommonDao commonDao;

    @Override
    public List<Region> getCity(Region region) {
        return commonDao.getCity(region);
    }

    @Override
    public List<Region> getCounty(Region region) {
        return commonDao.getCounty(region);
    }

    @Override
    public List<Region> getGrid(Region region) {
        return commonDao.getGrid(region);
    }

    @Override
    public List<Region> getSameGrade(Region region) {
        if (ObjectUtil.isNotNull(region)) {
            Region tempRegion = ObjectUtil.cloneIfPossible(region);
            if (ObjectUtil.isNotEmpty(region.getGridCode())) {
                tempRegion.setGridCode("00");
                return commonDao.getGrid(tempRegion);
            }
            if (ObjectUtil.isNotEmpty(region.getCountyCode())) {
                tempRegion.setCountyCode("00");
                return commonDao.getCounty(tempRegion);
            }
            if (ObjectUtil.isNotEmpty(region.getCityCode())) {
                tempRegion.setCityCode("00");
                return commonDao.getCity(tempRegion);
            }
        }
        return null;
    }
}
